import tkinter as tk
from tkinter import ttk

class ModernAppFrame(tk.Frame):
    def __init__(self, master, config=None, **kwargs):
        # 父类初始化与参数传递 
        super().__init__(master, **kwargs)
        self.master  = master
        self.config  = config or {"theme": "flatly"}  # 默认配置 
        self._init_ui()
        self._bind_events()

    def _init_ui(self):
        # 顶部工具栏
        self.toolbar  = ttk.Frame(self)
        self.btn_open  = ttk.Button(self.toolbar,  text="加载", command=self._on_open)
        self.btn_open.pack(side="left",  padx=2)

        # 主内容区
        self.content  = tk.Canvas(self, bg="#F5F5F5")
        self.scrollbar  = ttk.Scrollbar(self, orient="vertical", command=self.content.yview)
        self.content.configure(yscrollcommand=self.scrollbar.set)

        # 布局管理
        self.toolbar.pack(fill="x",  pady=5)
        self.content.pack(side="left",  fill="both", expand=True)
        self.scrollbar.pack(side="right",  fill="y")

    def _bind_events(self):
        # 鼠标滚轮滚动支持
        self.content.bind("<MouseWheel>",  self._on_scroll)
        # 窗口尺寸自适应
        self.bind("<Configure>",  lambda e: self.content.configure(scrollregion=self.content.bbox("all")))

    def _on_scroll(self, event):
        self.content.yview_scroll(int(-1*(event.delta/120)),  "units")

    def _on_open(self):
        """异步加载数据示例"""
        self.after(100,  lambda: self.insert_data("2025-03-22  19:35:00 加载完成"))

    def insert_data(self, text):
        # 动态生成带样式的文本块
        frame = ttk.Frame(self.content)
        label = ttk.Label(frame, text=text, style="TLabel")
        label.pack(anchor="w")
        self.content.create_window((20,  len(self.content.children)*30),  window=frame, anchor="nw")


class DarkModernAppFrame(ModernAppFrame):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.style  = ttk.Style()
        self.style.theme_use("alt")   # 使用暗色主题
        self.content.configure(bg="#2D2D2D")

    def _responsive_design(self):
        # 根据窗口尺寸调整布局
        if self.winfo_width()  < 600:
            self.toolbar.pack_forget()
            self.mobile_nav.pack(fill="x")
        else:
            self.mobile_nav.pack_forget()
            self.toolbar.pack(fill="x")

class MultiWindowManager:
    def __init__(self):
        self.root  = tk.Tk()
        self.frames  = {}

    def add_frame(self, name, frame_class):
        self.frames[name]  = frame_class(self.root)
        self.frames[name].pack_forget()

    def switch_frame(self, name):
        for frame in self.frames.values():
            frame.pack_forget()
        self.frames[name].pack(fill="both",  expand=True)

if __name__ == "__main__":
    root = tk.Tk()
    root.geometry("800x600")

    # 创建自定义窗体实例
    app_frame = ModernAppFrame(root, config={"theme": "material"})
    app_frame.pack(fill="both",  expand=True)

    # 动态测试
    app_frame.insert_data(" 初始化数据：当前时间 2025-03-22 19:35:00")
    root.mainloop()